// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.dengues.commons.notifications.IDenguesNotificationsManager;
import org.dengues.core.IWarehousePluginService;
import org.dengues.core.connectivity.IConnectivityProperty;
import org.dengues.core.property.NodeParameterUtils;
import org.dengues.core.sql.IDataEditor;
import org.dengues.core.warehouse.IWarehouseViewFactory;
import org.dengues.warehouse.dbmanager.ui.DataEditorComposite;
import org.dengues.warehouse.dbmanager.ui.action.DatabaseManageAction;
import org.dengues.warehouse.i18n.Messages;
import org.dengues.warehouse.notifications.DenguesNotificationsManager;
import org.dengues.warehouse.viewers.WarehouseViewFactory;
import org.dengues.warehouse.viewers.action.OpenPrefsSettingsAction;
import org.dengues.warehouse.viewers.properties.DescriptionSection;
import org.dengues.warehouse.viewers.properties.WarehouseViewPropsLabelProvider;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ListDialog;
import org.eclipse.ui.views.properties.tabbed.ISection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class WarehousePluginService implements IWarehousePluginService {

    class ListProvider extends LabelProvider implements ITreeContentProvider {

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
         */
        public Object[] getChildren(Object parentElement) {
            if (parentElement instanceof Set) {
                return ((Set) parentElement).toArray(new Object[0]);
            }
            return new Object[0];
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
         */
        public Object getParent(Object element) {
            return null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
         */
        public boolean hasChildren(Object element) {
            return false;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
         */
        public Object[] getElements(Object inputElement) {
            return getChildren(inputElement);
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
         * java.lang.Object, java.lang.Object)
         */
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

        }

    }

    public LabelProvider getViewPropsLabelProvider() {
        return new WarehouseViewPropsLabelProvider();
    }

    public IWarehouseViewFactory getWarehouseViewFactory() {
        return new WarehouseViewFactory();
    }

    public Action getOptionHsqldb() {
        return new DatabaseManageAction();
    }

    public Action getOpenPrefsDialog() {
        return new OpenPrefsSettingsAction();
    }

    public IDenguesNotificationsManager getDenguesNotificationsManager() {
        return new DenguesNotificationsManager();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IWarehousePluginService#getDescriptionSection()
     */
    public ISection getDescriptionSection(TabbedPropertySheetWidgetFactory widgetFactory) {
        return new DescriptionSection(widgetFactory);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IWarehousePluginService#openSelectConnSetDialog()
     */
    public IConnectivityProperty openSelectConnSetDialog(boolean cdc) {
        IConnectivityProperty connectivityProperty = null;
        ListDialog listDialog = new ListDialog(new Shell());
        listDialog.setTitle(Messages.getString("DatabaseManageAction.selectTitle")); //$NON-NLS-1$
        listDialog.setMessage(Messages.getString("DatabaseManageAction.top")); //$NON-NLS-1$
        ListProvider listProvider = new ListProvider();
        listDialog.setContentProvider(listProvider);
        listDialog.setLabelProvider(listProvider);
        Map<String, IConnectivityProperty> connectivityProperties = NodeParameterUtils.getConnectivityProperties();
        Set<String> keySet = connectivityProperties.keySet();

        Set<String> set = new HashSet<String>();
        for (String string : keySet) {
            if (cdc && connectivityProperties.get(string).isCDC()) {
                continue;
            }
            set.add(string);
        }
        listDialog.setInput(set);
        if (listDialog.open() == ListDialog.OK) {
            Object[] result2 = listDialog.getResult();
            if (result2 != null && result2.length == 1) {
                connectivityProperty = connectivityProperties.get(result2[0]);
            }
        }
        return connectivityProperty;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.IWarehousePluginService#createDataEditorComposite(org.eclipse.swt.widgets.Composite)
     */
    public IDataEditor createDataEditorComposite(Composite parent) {
        return new DataEditorComposite(parent);
    }
}
